//
//  YCJoinHEOBottomView.m
//  HeartWorldClient
//
//  Created by 董知樾 on 2016/11/16.
//  Copyright © 2016年 谭明权. All rights reserved.
//

#import "YCJoinHEOBottomView.h"
#import <CoreText/CoreText.h>

@interface YCJoinHEOBottomView ()<UITextViewDelegate>

@property (strong, nonatomic) UIButton *payBtn;

@property (copy, nonatomic) void(^didClickLinkBlock)(NSString *linkStr);

@property (copy, nonatomic) void(^setupPreviewBlock)(UITextView *textView);

@end

@implementation YCJoinHEOBottomView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createSubviews];
    }
    return self;
}

- (void)createSubviews {
    //110
    self.backgroundColor = [UIColor whiteColor];
    
    UITextView *textView = [[UITextView alloc]init];
    textView.font = [UIFont systemFontOfSize:12];
    
    textView.editable = NO;
    textView.delegate = self;
    [self addSubview:textView];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(2, 30, 2, 30));
    }];
    [self setCoreTextWithLabel:textView];
    
    _payBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _payBtn.backgroundColor = [UIColor colorWithRGBValue:0xcccccc];
    _payBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [_payBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_payBtn setTitle:@"充值" forState:UIControlStateNormal];
    [_payBtn addTarget:self action:@selector(didClickPayBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_payBtn];
    [_payBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.trailing.leading.bottom.equalTo(self);
        make.height.mas_equalTo(40);
    }];
    _payBtn.enabled = NO;
    _payBtn.hidden = YES;
}

- (void)setCoreTextWithLabel:(UITextView *)textView {
//    UIImage *tick_circle = [UIImage imageNamed:@"tick_circle"];
//    NSTextAttachment *tick_circleAttachment = [[NSTextAttachment alloc]initWithData:nil ofType:nil];
//    tick_circleAttachment.image = tick_circle;
//    tick_circleAttachment.bounds = CGRectMake(0, 0, 13, 13);
//    NSAttributedString *tick_circleStr = [NSAttributedString attributedStringWithAttachment:tick_circleAttachment];
    
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"tick_circle"]];
    [textView addSubview:imageView];
    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(11, 11));
        make.top.mas_equalTo(9.5);
        make.leading.mas_equalTo(8);
    }];
    
    NSMutableAttributedString * content = [[NSMutableAttributedString alloc]init];//WithAttributedString:tick_circleStr];;
    NSString *mainText = @"    我承诺加入会员身体健康，无癌症或者其他重大病史，并同意";
    NSString *hyperlinkText1 = @"《百倍爱心卡公约》";
    NSString *prepText = @"条款";
//    NSString *hyperlinkText2 = @"《健康承诺》";
//    NSRange range = NSMakeRange(content.length, hyperlinkText1.length);
    [content appendAttributedString: [[NSAttributedString alloc] initWithString: mainText attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRGBValue:0x999999]}]];
    [content appendAttributedString: [[NSAttributedString alloc] initWithString: hyperlinkText1 attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRGBValue:0xef4a65],NSLinkAttributeName:[NSURL URLWithString:help_each_other_link]}]];
    [content appendAttributedString: [[NSAttributedString alloc] initWithString: prepText attributes:@{NSForegroundColorAttributeName:[UIColor colorWithRGBValue:0x999999]}]];
//    [content appendAttributedString: [[NSAttributedString alloc] initWithString: hyperlinkText2 attributes:@{NSLinkAttributeName:[NSURL URLWithString:heath_promise_link]}]];
    
    textView.linkTextAttributes = @{
                                    NSForegroundColorAttributeName:[UIColor colorWithRGBValue:0xef4a65]
                                    };
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    paragraphStyle.alignment = NSTextAlignmentCenter;
    
    [content addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, content.length)];
    
    textView.attributedText = content;
    
}

//MARK:~~~~UITextViewDelegate~~~~
- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange {
    if ([URL.absoluteString isEqualToString:heath_promise_link] ||//健康承诺
        [URL.absoluteString isEqualToString:help_each_other_link]) {//互助条约
        if (_didClickLinkBlock) {
            _didClickLinkBlock(URL.absoluteString);
        }
    }
    return YES;
}

- (BOOL)textView:(UITextView *)textView shouldInteractWithURL:(NSURL *)URL inRange:(NSRange)characterRange interaction:(UITextItemInteraction)interaction {
    if (interaction == UITextItemInteractionInvokeDefaultAction) {
        if ([URL.absoluteString isEqualToString:heath_promise_link] ||//健康承诺
            [URL.absoluteString isEqualToString:help_each_other_link]) {//互助条约
            if (_didClickLinkBlock) {
                _didClickLinkBlock(URL.absoluteString);
            }
        }
    }
//    if (self.traitCollection.forceTouchCapability == UIForceTouchCapabilityAvailable) {
//        if (_setupPreviewBlock) {
//            _setupPreviewBlock(textView);
//        }
//    }
    
    return YES;
}

- (void)setMoney:(CGFloat)money {
    _money = money;
    [_payBtn setTitle: [NSString stringWithFormat:@"充值%.2f元",money] forState:UIControlStateNormal];
}

- (void)didClickPayBtn:(UIButton *)btn {
    if (_didClickLinkBlock) {
        _didClickLinkBlock(paybtn_click);
    }
}

- (void)didClickLinkWith:(void(^)(NSString *linkStr))block {
    _didClickLinkBlock = block;
}

- (void)setupPreview:(void(^)(UITextView *textView))block {
    _setupPreviewBlock = block;
}

- (void)setPayBtnEnable:(BOOL)enable {
    _payBtn.enabled = enable;
    _payBtn.backgroundColor = enable ? [UIColor colorWithRGBValue:0xef4a65] : [UIColor colorWithRGBValue:0xcccccc];
}

@end
